package queue_stack.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 最小栈
 * https://leetcode-cn.com/problems/min-stack/
 */
public class MinStack {
    // 实际存储元素的动态数组
    private List<Integer> list;
    // 辅助栈
    private Stack<Integer> stack;
    private int size;
    public MinStack() {
        list = new ArrayList<>();
        stack = new Stack<>();
    }

    public void push(int val) {
        list.add(val);
        if (stack.isEmpty()) {
            stack.push(val);
        }else {
            int tmpMin = stack.peek();
            int min = Math.min(val,tmpMin);
            stack.push(min);
        }
        size ++;
    }

    public void pop() {
        list.remove(--size);
        stack.pop();
    }

    public int top() {
        return list.get(size - 1);
    }

    public int getMin() {
        return stack.peek();
    }

}
